//
//  LTY_SingleSongViewController.m
//  MooMusic
//
//  Created by 守护神 on 15/10/27.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "LTY_SingleSongViewController.h"
// 导入NetWork头文件
#import "NetWork.h"
// 导入歌曲model的头文件
#import "LTY_SearchNextModel.h"

@interface LTY_SingleSongViewController ()<UITableViewDelegate, UITableViewDataSource>
// 创建tableview
@property (nonatomic, strong)UITableView *singleTableView;
// 设置一个数据源
@property (nonatomic, strong)NSMutableArray *dataArray;
// 设置一个用来存放url的数据源
@property (nonatomic, strong)NSMutableArray *urlListArray;

@property (nonatomic, strong) TabBarController *taBarSingle;
@end

static NSString *const cellIndf = @"cellIndf";

@implementation LTY_SingleSongViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.urlListArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"单曲";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建视图
    [self creatSingleTableView];
    // 获取数据
    [self getSingleData];
    
    // Do any additional setup after loading the view.
}
// 获取数据
- (void)getSingleData
{
    NSString *string = [NSString stringWithFormat:searchNextString, self.SearchStr];
    NSLog(@"+_+_+_+_  %@", string);
    // 请求数据
    [NetWork networkGETRequestWithURL:string Paramater:nil ResultBlock:^(id result) {
        
        
        if ([[result objectForKey:@"data"] count] == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有匹配数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:alert];
            [alert show];
        }else{
            
            NSMutableArray *data = [NSMutableArray array];
            data = [result objectForKey:@"data"];
            // 把model存放到数据源里
            self.dataArray = [LTY_SearchNextModel arrayWithModelByArray:data];
            
            for (NSDictionary *dic in data) {
                if ([[dic objectForKey:@"urlList"] count] > 0) {
                    [self.urlListArray addObject:[[[dic objectForKey:@"urlList"] objectAtIndex:0] valueForKey:@"url"]];
                }else{
                    [self.urlListArray addObject:@"http://om32.alicdn.com/135/135/2100216776/1774917404_58964790_l.m4a?auth_key=98149b7cb3c9f5981fc44469300f647e-1446336000-0-null"];
                }
                
            }

            
            // 刷新tableview
            if (self.dataArray.count > 0) {
                [self.singleTableView reloadData];
            }
            
            
            // 创建的时候使tableview的线显示
            self.singleTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            
            
        }
        
        
        
    }];
}

// 创建tableview
- (void)creatSingleTableView
{
    self.singleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
    self.singleTableView.dataSource = self;
    self.singleTableView.delegate = self;
    self.singleTableView.backgroundColor = [UIColor whiteColor];
    [self.singleTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIndf];
    // 创建的时候使tableview的线隐藏
    self.singleTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.singleTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndf];
    
    if (self.dataArray.count > 0) {
        LTY_SearchNextModel *model = [self.dataArray objectAtIndex:indexPath.row];
        //    关键字高亮
        NSString *str = model.name;
        //    初始化一个富文本属性
        NSMutableAttributedString *arrtiString = [[NSMutableAttributedString alloc]initWithString:str];
        //    搜索指定文字的范围
        NSRange range = [str rangeOfString:self.SearchStr];
        //    给富文本添加属性
        [arrtiString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
        cell.textLabel.attributedText = arrtiString;

    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSLog(@"把songId传递到播放页面 songId = %@", [self.dataArray[indexPath.row] valueForKey:@"songId"]);
    self.taBarSingle = [TabBarController shareTabBar];
//    [self.taBarSingle.array removeAllObjects];
    self.taBarSingle.array = [NSMutableArray array];
    self.taBarSingle.array = [NSMutableArray arrayWithArray:self.urlListArray];
    self.taBarSingle.index = indexPath.row;
//        self.taBarSingle.index = 0;
    [self.taBarSingle.tabBarView.songStopORStart setImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
    
    [self.taBarSingle play:[self.urlListArray objectAtIndex:indexPath.row]];
    
    // 更新tabBar内容
    self.taBarSingle.tabBarView.songerName.text = [self.dataArray[indexPath.row] singerName];
    self.taBarSingle.tabBarView.songName.text = [self.dataArray[indexPath.row] name];
    
//    self.taBarSingle.stopAngle = 10;
    [self.taBarSingle startAnimation];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
